var Searcher = {
	searcher: {
		current: 1, //默认值
		limit: 50,
		count: 0,
		text: '',
		leis: '',
		teams: [],
		textType: 0, //智能 商标名 注册号 申请人
		types: [], //'含中文', '含英文', '含数字', '纯图形'
		ageStart: '',
		ageEnd: '',
		sqType: '', //申请人类型
		sqNum: '',
	},
	flag: {
		finished: false,
		isLoading: false,
	},
	items: [],
	total: '',
	category: [], //分类数据
	//点击搜索按钮
	clickSearchBtn: function() {
		let text = bbk.getVal('search-keywords');
		if (/[^\u4e00-\u9fa5\w\s（）()]/.test(text)) {
			//去掉多余符号
			this.searcher.text = text.replace(/[^\u4e00-\u9fa5\w\s（）()·]/g, ' ');
			Msger.info('已去掉多余符号');
			$('#search-input').val(this.searcher.text);
		} else {
			this.searcher.text = text;
		}
		if (this.searcher.text.length > 20) {
			let tail = this.searcher.text.substring(20, this.searcher.text.length);
			this.searcher.text = this.searcher.text.substring(0, 20);
			Msger.info('关键词过长,已忽略「' + tail + '」');
		}
		this.searcher.current = 1;
		this.doSearch();
	},
	clickTextTypeBtn: function(obj) {
		$('li.search-type').removeClass('on');
		obj.className += ' on';
		this.searcher.textType = obj.getAttribute("value");
	},
	//搜索事件
	setListner: function() {
		let searcher = Searcher.searcher;
		$('#search-button').click(function() {
			Searcher.clickSearchBtn();
		});
		$('#search-keywords').keyup(function(e) {
			if (e.key == 'Enter') {
				Searcher.clickSearchBtn();
			}
		});
	},
	searchVue: {
		el: '.result-items-cube-box',
		data: {},
		methods: {
			isEmpty: function(str) {
				return bbk.isEmpty(str)
			},
			getPicUrl: function(item) {
				if( !bbk.isEmpty(item.thumb_image) ){
					return item.thumb_image + '?v=' + new Date().getTime();
				}
				console.info(item.thumb_image);
				return item.trademark_image;
			},
			getUrl: function(item) {
				return '/trademarks/detail/tid/' + item.id + '.html';
			},
			countGoods: function(item) {
				return '<span class="goods-num">' + item.count_goods_service + '个产品/服务</span>';
			}
		}
	},
	initVue: function() {
		this.searchVue.data.items = this.items;
		new Vue(this.searchVue);
	},
	renderHtml: function() {
		//排序
		var orderAcBtn = {
			id: 'param-order-by-box',
			names: ['价格由低到高', '同名标仅显示一个类别'],
			click: function(name, names, isChoosed) {
				let searcher = Searcher.searcher;
				switch (name) {
					case '价格由低到高':
						if (isChoosed) {
							searcher.orderBy = 1;
						} else {
							searcher.orderBy = 0;
						}
						break;
					case '同名标仅显示一个类别':
						if (isChoosed) {
							searcher.distinct = 1;
						} else {
							searcher.distinct = 0;
						}
						break;
				}
			}
		}
		AcBtn.render(orderAcBtn);
		//价格
		var priceAcBtn = {
			id: 'param-price-btn-box',
			names: ['0~3千', '3千~6千', '6千~1万', '1万~1.5万', '1.5万~2万', '2万~3万', '3万以上'],
			isSingle: true,
			click: function(name) {
				switch (name) {
					case '0~3千':
						Searcher.searcher.priceStart = 0;
						Searcher.searcher.priceEnd = 3000;
						ParamerNav.add('价格:' + name);
						break;
					case '3千~6千':
						Searcher.searcher.priceStart = 3000;
						Searcher.searcher.priceEnd = 6000;
						ParamerNav.add('价格:' + name);
						break;
					case '6千~1万':
						Searcher.searcher.priceStart = 6000;
						Searcher.searcher.priceEnd = 10000;
						ParamerNav.add('价格:' + name);
						break;
					case '1万~1.5万':
						Searcher.searcher.priceStart = 10000;
						Searcher.searcher.priceEnd = 15000;
						ParamerNav.add('价格:' + name);
						break;
					case '1.5万~2万':
						Searcher.searcher.priceStart = 15000;
						Searcher.searcher.priceEnd = 20000;
						ParamerNav.add('价格:' + name);
						break;
					case '2万~3万':
						Searcher.searcher.priceStart = 20000;
						Searcher.searcher.priceEnd = 30000;
						ParamerNav.add('价格:' + name);
						break;
					case '3万以上':
						Searcher.searcher.priceStart = 30000;
						Searcher.searcher.priceEnd = 1000000;
						ParamerNav.add('价格:' + name);
						break;
				}
				$('.param-price').val('');
				if (bbk.isEmpty(name)) {
					ParamerNav.remove('价格');
				}
			}
		}
		AcBtn.render(priceAcBtn);
		//商标类别
		var leiAcBtn = {
			id: 'param-lei-box',
			names: ['01-化学原料', '02-颜料油漆', '03-日化用品', '04-燃料油脂', '05-医药医药', '06-金属材料', '07-机械设备',
				'08-手工器械', '09-科学仪器', '10-医疗器械', '11-灯具空调', '12-运输工具', '13-军火烟火', '14-珠宝钟表', '15-乐器乐器',
				'16-办公用品', '17-橡胶制品', '18-皮革皮具', '19-建筑材料', '20-家具家具', '21-厨房洁具', '22-绳网袋蓬', '23-纱线丝带',
				'24-布料床单', '25-服装鞋帽', '26-钮扣拉链', '27-地毯席垫', '28-健身器材', '29-食品食品', '30-方便食品', '31-农林生鲜',
				'32-啤酒饮料', '33-酒酒酒酒', '34-烟草烟具', '35-广告销售', '36-金融物管', '37-建筑修理', '38-通讯服务', '39-运输贮藏',
				'40-材料加工', '41-教育娱乐', '42-科技服务', '43-餐饮住宿', '44-医疗园艺', '45-社会服务', '全选'
			],
			isSingle: false,
			click: function(name, names, isChoose) {
				let leis = [];
				for (let i = 0; i < names.length; i++) {
					let l = names[i].split('-')[0];
					leis.push(l);
				}
				leis.sort(); //排序
				if (name == '全选') {
					leis = [];
					if (isChoose) {
						for (let i = 1; i <= 45; i++) {
							leis.push(i);
						}
						AcBtn.chooseAll('param-lei-box');
					} else {
						AcBtn.clearChoosed('param-lei-box');
						Searcher.searcher.teams.removeAll();
					}
				}
				Searcher.searcher.leis = leis;
				//群组
				Paramer.showTeamBox();
				if (!isChoose) {
					//取消类的选择
					let teams = Cons.getTeams(parseInt(name));
					if (!bbk.isEmpty(teams)) {
						for (let i = 0; i < teams.length; i++) {
							Searcher.searcher.teams.remove(teams[i]);
						}
					}
				}
				ParamerNav.add('群组:' + Searcher.searcher.teams);
				//小项
				if (leis.length > 1) {
					ParamerNav.remove('产品/服务');
					$('#param-goods-input').val('').attr('disabled', 'disabled').attr('placeholder', '同名标搜索时暂不支持添加小项');
				} else {
					AcBtn.resetGoodsInput();
				}

				if (leis.length == 0) {
					ParamerNav.remove('类别');
				} else {
					leis.remove('全选');
					ParamerNav.add('类别:' + leis);
				}
			}
		}
		AcBtn.render(leiAcBtn);
		let sqNumAcBtn = {
			id: 'param-sq-num-box',
			names: ['1-50', '1-100', '1-150', '1-200', '201-999'],
			isSingle: true,
			click: function(name, names) {
				if (bbk.isEmpty(name)) {
					ParamerNav.remove('注册人申请量');
				} else {
					Searcher.searcher.sqNum = name;
					ParamerNav.add('注册人申请量:' + name);
				}
			}
		}
		AcBtn.render(sqNumAcBtn);
		//组合类型
		var typeAcBtn = {
			id: 'param-type-box',
			names: ['纯中文', '纯英文', '纯数字', '纯图形', '中文+英文', '中文+数字', '英文+数字'],
			click: function(name, names) {
				let searcher = Searcher.searcher;
				searcher.types = [];
				if (bbk.isEmpty(names)) {
					ParamerNav.remove('组合类型');
				} else {
					ParamerNav.add('组合类型:' + names);
					for (let i = 0; i < names.length; i++) {
						switch (names[i]) {
							case '纯中文':
								searcher.types.add(1);
								break;
							case '纯英文':
								searcher.types.add(4);
								break;
							case '纯数字':
								searcher.types.add(2);
								break;
							case '纯图形':
								searcher.types.add(3);
								break;
							case '中文+英文':
								searcher.types.add(5);
								break;
							case '中文+数字':
								searcher.types.add(6);
								break;
							case '英文+数字':
								searcher.types.add(7);
								break;
						}
					}
				}
			}
		}
		AcBtn.render(typeAcBtn);
		//商标字数
		var sizeAcBtn = {
			id: 'param-size-box',
			names: ['1个字', '2个字', '3个字', '4个字', '5个字', '6个字', '7个字及以上'],
			click: function(name, names) {
				if (names.length > 0) {
					let nameSize = [];
					for (let i = 0; i < names.length; i++) {
						nameSize.add(parseInt(names[i]));
					}
					ParamerNav.add('字数:' + nameSize);
					Searcher.searcher.nameSize = nameSize;
				} else {
					ParamerNav.remove('字数');
					Searcher.searcher.nameSize = [];
				}
			}
		}
		AcBtn.render(sizeAcBtn);
		//申请人类型
		let sqTypeAcBtn = {
			id: 'param-sq-type-box',
			names: ['仅公司', '仅个人'],
			isSingle: true,
			click: function(name) {
				if (bbk.isEmpty(name)) {
					ParamerNav.remove('申请人类型');
				} else {
					ParamerNav.add('申请人类型:' + name);
					switch (name) {
						case '仅公司':
							Searcher.searcher.sqType = 1;
							break;
						case '仅个人':
							Searcher.searcher.sqType = 2;
							break;
					}
				}
			}
		}
		AcBtn.render(sqTypeAcBtn);
		//注册年限
		var ageAcBtn = {
			id: 'param-age-box',
			names: ['1年以内', '1-2年', '2-3年', '3年以上'],
			isSingle: true,
			click: function(name) {
				if (bbk.isEmpty(name)) {
					ParamerNav.remove('年限');
				} else {
					ParamerNav.add('年限:' + name);
				}
				let s = Searcher.searcher;
				switch (name) {
					case '1年以内':
						s.ageStart = 0;
						s.ageEnd = 1;
						break;
					case '1-2年':
						s.ageStart = 1;
						s.ageEnd = 2;
						break;
					case '2-3年':
						s.ageStart = 2;
						s.ageEnd = 3;
						break;
					case '3年以上':
						s.ageStart = 3;
						s.ageEnd = '';
						break;
					default:
						s.ageStart = '';
						s.ageEnd = '';
				}
			}
		}
		AcBtn.render(ageAcBtn);
		//名称结构:
		let abTypeAcbtn = {
			id: 'param-ab-type-box',
			names: ['AA', 'AB', 'ABB', 'AAB', 'AAA', 'AAAA', 'AABB', 'ABAB', 'ABBB', 'AAAB', 'ABAC', 'AABC', 'ABCC'],
			click: function(name, names) {
				if (names.length > 0) {
					let abTypesList = [];
					let abTypes = [];
					for (let i = 0; i < names.length; i++) {
						abTypesList.add(names[i]);
						switch (names[i]) {
							case 'AA':
								abTypes.add(1);
								break;
							case 'AB':
								abTypes.add(2);
								break;
							case 'ABB':
								abTypes.add(3);
								break;
							case 'AAB':
								abTypes.add(4);
								break;
							case 'AAA':
								abTypes.add(5);
								break;
							case 'AAAA':
								abTypes.add(6);
								break;
							case 'AABB':
								abTypes.add(7);
								break;
							case 'ABAB':
								abTypes.add(8);
								break;
							case 'ABBB':
								abTypes.add(9);
								break;
							case 'AAAB':
								abTypes.add(10);
								break;
							case 'ABAC':
								abTypes.add(11);
								break;
							case 'AABC':
								abTypes.add(12);
								break;
							case 'ABCC':
								abTypes.add(13);
								break;
						}
					}
					ParamerNav.add('名称结构:' + abTypesList);
					Searcher.searcher.abTypes = abTypes;
				} else {
					ParamerNav.remove('名称结构');
					Searcher.searcher.abTypes = [];
				}
			}
		}
		AcBtn.render(abTypeAcbtn);
		//小项个数:
/*		let xxNumAcbtn = {
			id: 'param-xx-num-box',
			isSingle: true,
			names: ['1-3', '4-6', '6-9', '10-100'],
			click: function(name) {
				if (bbk.isEmpty(name)) {
					Searcher.searcher.xxNum = '';
					ParamerNav.remove('小项个数');
				} else {
					Searcher.searcher.xxNum = name;
					ParamerNav.add('小项个数:' + name);
				}

			}
		}
		AcBtn.render(xxNumAcbtn);*/
		//去掉暂时无法转让的商标
/*		let blackSqNamerAcBtn = {
			id: 'param-black-sq-namer-box',
			names: ['保留暂时无法转让的商标'],
			isSingle: true,
			click: function(name, names) {
				if (bbk.isEmpty(name)) {
					//取消操作
					ParamerNav.remove('保留暂时无法转让的商标');
					Searcher.searcher.blackSqNamer = 0;
				} else {
					Searcher.searcher.blackSqNamer = '';
					ParamerNav.add('保留暂时无法转让的商标:是');
				}
			}
		}
		AcBtn.render(blackSqNamerAcBtn);*/
	},
	getSearchModeData: function (){
		return [{
			title: '开头包含',
			code: 1
		}, {
			title: '结尾包含',
			code: 2
		}, {
			title: '模糊搜索(默认)',
			code: 3
		}, {
			title: '精准搜索',
			code: 4
		}];
	},
	initSearchMode: function() {
		let searchModes = Searcher.getSearchModeData();
		layui.dropdown.render({
			elem: '#search-mode',
			data: Searcher.getSearchModeData(),
			click: function(data, othis) {
				$(this.elem).html(data.title);
				$(this.elem).css('color', '#fff').css('background', '#3f56e0');
				Searcher.searcher.searchMode = data.code;
			}
		});
	},
	init: function(initSearcher,initCategory) {
		//参数合并
		Object.assign(this.searcher,initSearcher);
		this.category = initCategory;
		//执行操作
		this.setListner();
		this.initVue();
		this.renderHtml();
		this.initSearchMode();
		//设置默认值
		this.initSetDefault();
		//请求数据
		this.doSearch();
	},
	initSetDefault: function (){
		let searcher = this.searcher;
		//搜索模式
		this.getSearchModeData().forEach(function(o,i){
			if(searcher.searchMode == o.code){
				$('li#search-mode').html(o.title).css('color', '#fff').css('background', '#3f56e0');
			}
		});
		//搜索类型
		if(searcher.textType >0){
			$(".search-item li.search-type[value='"+searcher.textType+"']").click();
		}
		//搜索关键字
		if(searcher.text){
			$('#search-keywords').val(searcher.text);
		}
		//注册人申请量
		$('ul#param-sq-num-box li').each(function (index,obj){
			let _this = $(obj);
			if(_this.find('span').html() == searcher.sqNum){
				_this.click();
			}
		})
	},
	//搜索开始
	start: function() {
		this.flag.isLoading = true;
		$('.none-result').html('正在搜索...');
	},
	//搜索完成
	finish: function() {
		this.flag.isLoading = false;
		if (bbk.isEmpty(Searcher.items)) {
			$('.none-result').html('没有查询到任何结果,请更换搜索条件');
			$('#laypage-box').css('display', 'none');
		} else {
			$('.none-result').html('');
			$('#laypage-box').css('display', 'block');
		}
	},
	//执行搜索
	doSearch: function() {
		if(this.flag.isLoading){
			return;
		}
		let ajax = {
			url: '/index/search.html',
			data: Searcher.searcher,
			success: function(res) {
				let branders = res.data.results;
				//let category = res.data.category;
				Searcher.total = res.data.total > 0 ? res.data.total:Searcher.total;
				//清空
				Searcher.items.removeAll();
				for (let i = 0; i < branders.length; i++) {
					branders[i]['cate_info'] = Searcher.category[branders[i]['trademark_cate'] - 1];
					Searcher.items.push(branders[i]);
				}
				Searcher.randerPage();
				Searcher.finish();
			},
			error: function(res) {
				Searcher.finish();
			}
		}
		this.start();
		bbk.ajax(ajax);
	},
	randerPage: function() {
		layui.use('laypage', function() {
			var laypage = layui.laypage;
			//执行一个laypage实例
			laypage.render({
				elem: 'laypage-box',
				count: Searcher.total, //数据总数，从服务端得到
				curr: Searcher.searcher.current,
				limits: [50, 100, 200],
				groups: 3,
				layout: ['count', 'page', 'prev', 'next', 'limit', 'skip'],
				limit: Searcher.searcher.limit,
				jump: function(obj, first) {
					//首次不执行
					if (!first) {
						Searcher.searcher.current = obj.curr;
						Searcher.searcher.limit = obj.limit;
						Searcher.doSearch();
					}
				}
			});
		});
	},
	inputClear: function() {
		$('#search-keywords').val('');
	},
	setText: function() {
		this.searcher.text = bbk.getVal('search-keywords');
	}
}

//条件导航
ParamerNav = {
	names: [],
	add: function(name) {
		let label = name.split(':')[0];
		let content = name.split(':')[1];
		if (bbk.isEmpty(content)) {
			this.remove(label);
			return;
		}
		let isExit = false;
		for (let i = 0; i < this.names.length; i++) {
			if (this.names[i].split(':')[0] == label) {
				this.names[i] = name;
				isExit = true;
			}
		}
		if (!isExit) {
			this.names.push(name);
		}
		this.render();
	},
	//清空总按钮
	removeAll: function() {
		while (this.names.length > 0) {
			this.remove(this.names[0].split(":")[0]);
		}
		ChooseItemer.removeAll();
		Searcher.inputClear();
		AcBtn.resetGoodsInput();
		AcBtn.unlimitChoosedAll();
		Msger.info('已清空');
	},
	remove: function(label) {
		for (let i = 0; i < this.names.length; i++) {
			if (this.names[i].indexOf(label) > -1) {
				this.names.splice(i, 1);
				i = this.names.length;
				this.render();
			}
		}
		let searcher = Searcher.searcher;
		switch (label) {
			case '价格':
				searcher.priceStart = '';
				searcher.priceEnd = '';
				$('#param-price-start').val('');
				$('#param-price-end').val('');
				AcBtn.clearChoosed('param-price-btn-box');
				break;
			case '类别':
				searcher.leis = [];
				searcher.teams = [];
				AcBtn.clearChoosed('param-lei-box');
				ParamerNav.remove('群组');
				Paramer.showTeamBox();
				break;
			case '群组':
				searcher.teams = [];
				for (let i = 1; i < 46; i++) {
					AcBtn.clearChoosed('param-team-' + i);
				}
				break;
			case '产品/服务':
				searcher.goods = '';
				$('#param-goods-input').val('');
				break;
			case '组合类型':
				searcher.types = [];
				AcBtn.clearChoosed('param-type-box');
				break;
			case '名称结构':
				searcher.abTypes = [];
				AcBtn.clearChoosed('param-ab-type-box');
				break;
			case '字数':
				searcher.nameSize = [];
				AcBtn.clearChoosed('param-size-box');
				break;
			case '小项个数':
				searcher.xxNum = '';
				AcBtn.clearChoosed('param-xx-num-box');
				break;
			case '注册人申请量':
				searcher.sqNum = '';
				AcBtn.clearChoosed('param-sq-num-box');
				break;
			case '申请人类型':
				searcher.sqType = '';
				AcBtn.clearChoosed('param-sq-type-box');
				break;
			case '年限':
				searcher.ageStart = '';
				searcher.ageEnd = '';
				AcBtn.clearChoosed('param-age-box');
				break;
			case '保留暂时无法转让的商标':
				AcBtn.clearChoosed('param-black-sq-namer-box');
				searcher.blackSqNamer = 0;
				break;
		}
	},
	render: function() {
		let acBtn = {
			id: 'search-param-nav-box',
			names: ParamerNav.names,
			canClose: true,
			close: function(name, names) {
				let label = name.split(':')[0];
				ParamerNav.remove(label);
			}
		}
		AcBtn.render(acBtn);
		if (ParamerNav.names.length > 0) {
			$('.clear-btn').css('display', 'block');
		} else {
			$('.clear-btn').css('display', 'none');
		}
	}
}

Paramer = {
	price: function() {
		let start = bbk.getVal('param-price-start');
		let end = bbk.getVal('param-price-end');
		Searcher.searcher.priceStart = bbk.getPrice(start);
		Searcher.searcher.priceEnd = bbk.getPrice(end) == 0 ? '1000000' : bbk.getPrice(end);
		ParamerNav.add('价格:' + Searcher.searcher.priceStart + ' - ' + Searcher.searcher.priceEnd);
		AcBtn.clearChoosed('param-price-btn-box');
	},
	showTeamBox: function() {
		let leis = Searcher.searcher.leis;
		if (leis.length == 0) {
			$('#param-team-00').css('display', 'block');
		} else {
			$('#param-team-00').css('display', 'none');
		}
		for (let i = 1; i < 46; i++) {
			if (leis.indexOf(i) > -1) {
				if (bbk.isEmpty($('#param-team-' + i).html())) {
					AcBtn.render({
						id: 'param-team-' + i,
						names: Cons.getTeams(i),
						titles: Cons.getTitles(i),
						click: function(name, names, isChoose) {
							let teams = Searcher.searcher.teams;
							if (isChoose) {
								teams.add(name);
							} else {
								teams.remove(name);
								teams.sort();
							}
							teams.sort();
							if (teams.length == 0) {
								ParamerNav.remove('群组');
							} else {
								ParamerNav.add('群组:' + teams);
							}
						}
					});
				}
			} else {
				$('#param-team-' + i).html('');
			}
		}
	},
	goods: function() {
		let goods = bbk.getVal('param-goods-input');
		let goodsArr = [];
		goods = goods.split(' ');
		for (let i = 0; i < goods.length; i++) {
			if (!bbk.isEmpty(goods[i])) {
				goodsArr.push(goods[i]);
			}
		}
		Searcher.searcher.goods = goodsArr;
		ParamerNav.add('产品/服务:' + goodsArr);
	}
}

//每个页面选择的商标独立
ChooseItemer = {
	ids: [],
	add: function(id) {
		if (ChooseItemer.ids.length < 1000) {
			ChooseItemer.ids.add(id);
		} else {
			Msger.info('已超过1000个上限');
		}
	},
	remove: function(id) {
		ChooseItemer.ids.remove(id);
	},
	removeAll: function() {
		ChooseItemer.ids.removeAll();
	},
	isChoosed: function(id) {
		return ChooseItemer.ids.indexOf(id) > -1;
	},
	//是否当页已全选
	renderAllBtn: function() {
		let isAll = true;
		for (let i = 0; i < Searcher.items.length; i++) {
			if (ChooseItemer.ids.indexOf(Searcher.items[i].id) == -1) {
				isAll = false;
				break;
			}
		}
	}
}

window.onload = function() {
	Searcher.init(initSearcher,initCategory);
	$("#goods-box-content").show();
}
